<template>
  <div id="UserLoginPage">
    <h2 style="margin-bottom: 16px">用户登录</h2>
    <a-form
      :model="form"
      :style="{ width: '480px', margin: '0 auto' }"
      label-align="left"
      auto-label-width
      @submit="handleSubmit"
    >
      <a-form-item field="userAccount" label="账号">
        <a-input v-model="form.userAccount" placeholder="请输入账号" />
      </a-form-item>
      <a-form-item field="userPassword" tooltip="密码不能小于8位" label="密码">
        <a-input-password
          v-model="form.userPassword"
          placeholder="请输入密码"
        />
      </a-form-item>
      <a-form-item>
        <div
          style="
            display: flex;
            width: 100%;
            align-items: center;
            justify-content: space-around;
          "
        >
          <a-button html-type="submit" style="width: 120px">登录</a-button>
          <a-link @click="gotoRegister">注册</a-link>
        </div>
      </a-form-item>
    </a-form>
  </div>
</template>

<script setup lang="ts">
import { reactive } from "vue";
import { userLoginUsingPost } from "@/api/userController";
import { useLoginUserStore } from "@/store/userStore";
import meassage from "@arco-design/web-vue/es/message";
import API from "@/api";
import { useRouter } from "vue-router";

const loginUserStore = useLoginUserStore();
const router = useRouter();
const form = reactive({
  userAccount: "",
  userPassword: "",
} as API.UserLoginRequest);
/**
 * 登录提交
 * @param data
 */
const handleSubmit = async () => {
  const res = await userLoginUsingPost(form);
  if (res.data.code === 0) {
    await loginUserStore.fetchLoginUser();
    meassage.success("登录成功");
    router.push({
      path: "/",
      replace: true,
    });
  } else {
    meassage.error("登录失败," + res.data.message);
  }
};

const gotoRegister = () => {
  router.push({
    path: "/user/register",
    replace: true,
  });
};
</script>
